/*
 * @Descripttion: 
 * @version: 
 * @Author: lily
 * @Date: 2021-04-16 09:38:18
 * @LastEditors: lily
 * @LastEditTime: 2021-04-16 10:33:08
 */
/*
 * @lc app=leetcode.cn id=48 lang=javascript
 *
 * [48] 旋转图像
 */

// @lc code=start
/**
 * @param {number[][]} matrix
 * @return {void} Do not return anything, modify matrix in-place instead.
 */
//  思路：找规律
//  https://leetcode-cn.com/problems/rotate-image/solution/xuan-zhuan-tu-xiang-by-leetcode-solution-vu3m/
//  旋转数组 = 原数组 水平翻转 + 对角线翻转
//  复杂度：O(n2)  O(1)

var rotate = function (matrix) {
    const n = matrix.length;
    // 水平翻转
    for (let i = 0; i < Math.floor(n / 2); i++) {
        for (let j = 0; j < n; j++) {
            [matrix[i][j], matrix[n - i - 1][j]] = [matrix[n - i - 1][j], matrix[i][j]];
        }
    }
    // 主对角线翻转
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < i; j++) {
            [matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
        }
    }

    // // 暴力法   复杂度：O(n2)  O(n2)
    // let len = matrix.length
    // // 深拷贝
    // let arr = JSON.parse(JSON.stringify(matrix))
    // for (let i = 0; i < len; i++) {
    //     for (let j = 0; j < len; j++) {
    //         matrix[i][j] = arr[len - j - 1][i]
    //     }
    // }
};
// @lc code=end

rotate([[1, 2, 3], [4, 5, 6], [7, 8, 9]])